package com.gduf.lsm.service;

import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.JoinEventsStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.JoinActivity;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.JoinActivityMapper;
import com.gduf.lsm.pojo.dto.JoinStatusDTO;
import com.gduf.lsm.pojo.vo.JoinActivityShowVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lsm
 * @Data
 * 公益活动报名表
 */
@Service
public class JoinActivityService {
    @Autowired
    private JoinActivityMapper joinActivityMapper;

    /**
     * 新增
     * @param joinActivity
     */
    public void add(JoinActivity joinActivity) {
        //公益活动状态不可为空，添加默认状态为已报名
        //System.out.println("****************"+joinActivity);
        if(ObjectUtil.isEmpty(joinActivity.getStatus())){
            joinActivity.setStatus(JoinEventsStatusConstant.JOIN_EVENTS_STATUS_REGISTERED);
        }
        //System.out.println("****************"+joinActivity);
        joinActivityMapper.insert(joinActivity);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        joinActivityMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            joinActivityMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param joinActivity
     */
    public void updateById(JoinActivity joinActivity) {
        joinActivityMapper.updateById(joinActivity);
    }

    /**
     * 修改活动状态
     * @param joinStatusDTO
     */
    public void updateStatus(JoinStatusDTO joinStatusDTO) {
        //未通过状态下备注不可为空
        if(joinStatusDTO.getStatus().equals(JoinEventsStatusConstant.JOIN_EVENTS_STATUS_NOT_PASSED)&&ObjectUtil.isEmpty(joinStatusDTO.getRemark())){
            throw new CustomException(ResultCodeEnum.JOIN_ACTIVITY_REMARK_NULL_ERROR);
        }
        //把前端实体拷贝，因为有数字所以要一个个设置，避免数字被赋值为空
        JoinActivity joinActivity=new JoinActivity();
        joinActivity.setId(joinStatusDTO.getId());
        joinActivity.setStatus(joinStatusDTO.getStatus());
        joinActivity.setRemark(joinStatusDTO.getRemark());
        joinActivityMapper.updateById(joinActivity);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public JoinActivity selectById(Integer id) {
        return joinActivityMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param joinActivity
     * @return
     */
    public List<JoinActivity> selectAll(JoinActivity joinActivity) {
        return joinActivityMapper.selectAll(joinActivity);
    }

    /**
     * 分页查询
     * @param joinActivity
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<JoinActivityShowVO> selectPage(JoinActivity joinActivity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JoinActivityShowVO> list=joinActivityMapper.selectPageQuery(joinActivity);
        return PageInfo.of(list);
    }


    /**
     * 按用户ID查找参与过的活动，映射实体为VO
     * @param userId
     * @return
     */
    public List<JoinActivityShowVO> selectByUserId(Integer userId) {
        JoinActivity joinActivity=new JoinActivity();
        joinActivity.setUserId(userId);
        List<JoinActivityShowVO> list=joinActivityMapper.selectPageQuery(joinActivity);
        return list;
    }
}
